/**
 * ListePlansVols read the flight plans XML file and build a std::vector of them.
 */
#include <iostream>
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include "ListePlansVols.h"
#include "PlanVol.h"


void ListePlansVols::createTree(const char *filename) {
  doc = xmlReadFile(filename, NULL, 0);
  if (doc == NULL) {
    fprintf(stderr, "Failed to parse %s\n", filename);
    return;
  }
  // to free the document tree
  //xmlFreeDoc(doc);
}

void ListePlansVols::printElementNames(xmlNode * a_node)
{
  xmlNode *cur_node = NULL;
  
  for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
    if (cur_node->type == XML_ELEMENT_NODE) {
      printf("node type: Element, name: %s\n", cur_node->name);
      if (cur_node->properties != NULL)
	printf("properties: %s\t value: %s\n",
	       cur_node->properties->name,
	       cur_node->properties->children->content);
    }
    
    printElementNames(cur_node->children);
  }
}


PlanVol ListePlansVols::createPlanVol(xmlNode * node)
{
  PlanVol pl;
  
  //  printf("\n\nnoeud: %s,", node->name);
  if (node->properties != NULL)
    {
      //printf("  idVol: %s\n",node->properties->children->content);
       pl.setIdVol(atoi((char*)node->properties->children->content));
    }
  
  node = node->children;
  while (node)
    {
      if (node->type == XML_ELEMENT_NODE)
	{       
	  
	  if (strcmp("idAeroportDeDepart",(char*)node->name) == 0)
	    {
	      //printf("%s: %s\n",node->name,node->children->content);
	      char* idadd = (char*)node->children->content;
	      pl.setIdAeroportDeDepart(idadd);
	    }
	  if (strcmp("heureDeDepart",(char*)node->name) == 0)
	    {
	      //printf("%s: %s\n",node->name,node->children->content);
	      int hdd = atoi((char*)node->children->content);
	      pl.setHeureDeDepart(hdd);
	    }
	  if (strcmp("idAeroportDArrivee",(char*)node->name) == 0)
	   {
	     //printf("%s: %s\n",node->name,node->children->content);
	      char* idarr = (char*)node->children->content;
	      pl.setIdAeroportDArrivee(idarr);
	   }
	  if (strcmp("heureDArrivee",(char*)node->name) == 0)
	    {
	      //printf("%s: %s\n",node->name,node->children->content);
	      int harr = atoi((char*)node->children->content);
	      pl.setHeureDArrivee(harr);
	    }
	  if (strcmp("niveauDeVol",(char*)node->name) == 0)
	    {
	      //printf("%s: %s\n",node->name,node->children->content); 
	      int nivo = atoi((char*)node->children->content);
	      pl.setNiveauDeVol(nivo);
	    }
	}
      

      // go to next child
      node = node->next;
    }

  return pl;
}

ListePlansVols::ListePlansVols(char * filename) {
  xmlNode *rootElement, *vol;
  PlanVol pl;

  // to create the tree from the xml document
  createTree(filename);
  
  rootElement = xmlDocGetRootElement(doc);

  //printElementNames(rootElement);

  vol = rootElement->children->next;

  while (vol)
    {
      if (vol->type == XML_ELEMENT_NODE){
	pl = createPlanVol(vol);
	pl.afficher();
	vPlansVols.push_back(pl);
      }
      
      // go to next vol
      vol = vol->next;
    }
  printf("taille vPlansVols: %d\n",vPlansVols.size());
  
  xmlFreeDoc(doc);

  /*
   * Cleanup function for the XML library.
   */
  xmlCleanupParser();
  /*
   * this is to debug memory for regression tests
   */
  xmlMemoryDump();
}

ListePlansVols::~ListePlansVols()
{
}
